/**
 * 
 */
package com.nwpusp.controller.actions;

import java.util.ArrayList;

import com.nwpusp.model.GameModel;
import com.nwpusp.model.location.Axis;
import com.nwpusp.model.players.Player;
import com.nwpusp.model.session.ConnectionId;

/**
 * @author JustinCao
 *
 */
public class EnterGameReqAction extends SyncReqAction {

	public final static int localSyncId = 1024;
	public ArrayList<ConnectionId> cids = null;
	public ArrayList<Axis> axes = null;
	
	public EnterGameReqAction(GameModel model, ConnectionId monitorCid, ArrayList<ConnectionId> cids, ArrayList<Axis> axes) {
		super(monitorCid, localSyncId);
		this.cids = cids;
		this.axes = axes;
	}
	
	@Override
	public boolean OnSyncResponse(GameModel model, ConnectionId rspCid) {
		Player p = model.getPlayerManager().getPlayer(rspCid);
		if(p != null) {
			p.setStatus(Player.PlayerStatus.waitingAndResponse);
		}
		if(model.getPlayerManager().isAllPlayerOfStatus(Player.PlayerStatus.waitingAndResponse)) {
			model.startAllGame();
			return true;
		}
		return false;
	}

	@Override
	public void Invoke(GameModel m) {
		m.getPlayerManager().setAllPlayerStatus(Player.PlayerStatus.waiting);
		if(cids != null && axes != null) {
			for(int i = 0; i < this.cids.size(); i++) {
				Player p = m.getPlayerManager().getPlayer(cids.get(i));
				Axis a = axes.get(i);
				if(p != null && a != null) {
					p.getCh().setPosition(a);
				}
			}
		}
		m.enterGameViewLocal();
	}
}
